package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.UmsResourceMapper;
import com.mail.model.db.UmsResource;
import com.mail.model.dto.ResouceDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 吕永乐
 * @date 2022/06/23 11:40
 **/
@DubboService
public class UmsResourceApiImpl implements UmsResourceApi{

	@Autowired
	private UmsResourceMapper umsResourceMapper;

	/**
	 * 查询所有后台资源
	 *
	 * @return
	 */
	@Override
	public List<UmsResource> getAll() {
		return umsResourceMapper.getAll();
	}

	/**
	 * 根据 id 集合批量查询
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public List<UmsResource> getByIds(List<Long> ids) {
		return umsResourceMapper.selectBatchIds(ids);
	}

	/**
	 * 分页模糊查询后台资源
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param categoryId
	 * @param nameKeyword
	 * @param urlKeyword
	 * @return
	 */
	@Override
	public Page<UmsResource> listByPage(Long pageNum, Long pageSize, Long categoryId, String nameKeyword, String urlKeyword) {
		Page<UmsResource> page = new Page<>(pageNum, pageSize);
		LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(categoryId != null, UmsResource::getCategoryId, categoryId)
				.like(StringUtils.isNotEmpty(nameKeyword), UmsResource::getName, nameKeyword)
				.like(StringUtils.isNotEmpty(urlKeyword), UmsResource::getUrl, urlKeyword);
		return umsResourceMapper.selectPage(page, wrapper);
	}

	// 添加后台资源
	@Override
	public void createResource(UmsResource umsResource) {
		umsResourceMapper.insert(umsResource);
	}

	// 根据ID删除后台资源
	@Override
	public void deleteResourceById(Long id) {
		umsResourceMapper.deleteById(id);
	}

	// 修改后台资源
	@Override
	public void updateResource(Long id,UmsResource umsResource) {
		LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UmsResource::getId,id);
		umsResourceMapper.update(umsResource,wrapper);
	}

	// 根据ID获取资源详情
	@Override
	public UmsResource queryResourceById(Long id) {
		LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UmsResource::getId,id);
		UmsResource umsResource = umsResourceMapper.selectOne(wrapper);
		return umsResource;
	}
}
